/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package agent;

import java.util.ArrayList;

/**
 *
 * @author chuda
 */
import messages.AgentAdvertisment;
import mobileipsimulation.AgentInterface;

public class Agent {

    public static AgentInterface ai;
    private String ip = "";
    /*List of home guys*/
    private ArrayList<MobilityBindingEntry> mobilityBindingEntries = null;
    /*List of guest guys*/
    private ArrayList<VisitorEntry> visitorEntries = null;
    private BroadCastAdvertismentSender bcs;
    private AgentSender sender;
    private AgentReceiver receiver;
    private AgentAdvertisment agAdvert;
    private String flag;
    private int agentPort;
    private TimeVerifier tm;

    public Agent(String ip, String flag, int port, AgentInterface agi) {
        ai = agi;
       // ai.printMe("created java obj:"+flag);
        this.agentPort = port;
        agAdvert = new AgentAdvertisment(this.agentPort);
        agAdvert.addCareofAdress(port + "");
        this.flag = flag;
        agAdvert.setFlag(flag);
        this.ip = ip;
        System.out.println("agent " + flag + " ip: " + ip);
        mobilityBindingEntries = new ArrayList<MobilityBindingEntry>();
        visitorEntries = new ArrayList<VisitorEntry>();
//        if (flag.equals("home")) {
//            visitorEntries.add(new VisitorEntry("localhost", ip, "localhost", 2345));
//            // bcs = new BroadCastAdvertismentSender(2345, ip, agAdvert.toString());
//        }
        bcs = new BroadCastAdvertismentSender(2345, ip, agAdvert.toString(),ai,this.flag);
        receiver = new AgentReceiver(agentPort, this);
        tm = new TimeVerifier(this);
        System.out.println("I am running with ip " + this.getIp());
        
    }
    public AgentInterface getAi(){
        return ai;
    }
    public static void print(AgentInterface ai) {
        ai.printMe("hey!");
    }

    /**
     * @return the ip
     */
    public String getIp() {
        return ip;
    }

    /**
     * @param ip the ip to set
     */
    public void setIp(String ip) {
        this.ip = ip;
    }

    /**
     * @return the mobilityBindingEntries
     */
    public ArrayList<MobilityBindingEntry> getMobilityBindingEntries() {
        return mobilityBindingEntries;
    }

    /**
     * @param mobilityBindingEntries the mobilityBindingEntries to set
     */
    public void setMobilityBindingEntries(ArrayList<MobilityBindingEntry> mobilityBindingEntries) {
        this.mobilityBindingEntries = mobilityBindingEntries;
    }

    /**
     * @return the visitorEntries
     */
    public ArrayList<VisitorEntry> getVisitorEntries() {
        return visitorEntries;
    }

    /**
     * @param visitorEntries the visitorEntries to set
     */
    public void setVisitorEntries(ArrayList<VisitorEntry> visitorEntries) {
        this.visitorEntries = visitorEntries;
    }

    /**
     * @param visitorEntry the visitorEntry to add
     */
    public void addVisitorEntry(VisitorEntry visitorEntry) {
        if (this.visitorEntries == null) {
            visitorEntries = new ArrayList<VisitorEntry>();
        }
        visitorEntries.add(visitorEntry);
        setVisitorEntries(visitorEntries);
    }

    /**
     * @param mobilityBindingEntry the visitorEntry to add
     */
    public void addMobilityBindingEntry(MobilityBindingEntry mobilityBindingEntry) {
        if (this.mobilityBindingEntries == null) {
            mobilityBindingEntries = new ArrayList<MobilityBindingEntry>();
        }
        mobilityBindingEntries.add(mobilityBindingEntry);
        setMobilityBindingEntries(mobilityBindingEntries);
    }

    /**
     * @param mobilityBindingEntry the visitorEntry to remove
     */
    public void removeMobilityBindingEntry(MobilityBindingEntry mobilityBindingEntry) {
        if (this.mobilityBindingEntries.contains(mobilityBindingEntry)) {
            mobilityBindingEntries.remove(mobilityBindingEntry);
            setMobilityBindingEntries(mobilityBindingEntries);
        }
    }

    /**
     * @param visitorEntry the visitorEntry to remove
     */
    public void removeVisitorEntry(VisitorEntry visitorEntry) {
        if (this.visitorEntries.contains(visitorEntry)) {
            visitorEntries.remove(visitorEntry);
            setVisitorEntries(visitorEntries);
        }
    }

    public void removeVisitorEntryByIP(String ip) {
        if (this.containsVisitorEntry(ip)) {
            for (VisitorEntry ve : visitorEntries) {
                if (ve.getHomeAddress().equals(ip)) {
                    visitorEntries.remove(ve);
                    setVisitorEntries(visitorEntries);
                    break;
                }
            }
        }
    }

    public void removeMobilityBindingEntryByIP(String ip) {
        if (this.containsMobiltyBindingEntry(ip)) {
            for (MobilityBindingEntry mbe : mobilityBindingEntries) {
                if (mbe.getHomeAddress().equals(ip)) {
                    mobilityBindingEntries.remove(mbe);
                    setMobilityBindingEntries( mobilityBindingEntries);
                    break;
                }
            }
        }
    }

    public void sendMessage(String ipToSend, String mensagem, int porto) {
        sender = new AgentSender(porto, ipToSend, mensagem);
    }

    /**
     * @return the flag
     */
    public String getFlag() {
        return flag;
    }

    /**
     * @param flag the flag to set
     */
    public void setFlag(String flag) {
        this.flag = flag;
    }

    public boolean containsMobiltyBindingEntry(String ip) {
        for (MobilityBindingEntry mobilityBindingEntry : mobilityBindingEntries) {
            if (mobilityBindingEntry.getHomeAddress().equals(ip)) {
                return true;
            }
        }
        return false;
    }

    public boolean containsVisitorEntry(String ip) {
        for (VisitorEntry visitorEntry : visitorEntries) {
            if (visitorEntry.getHomeAddress().equals(ip)) {
                return true;
            }
        }
        return false;
    }
}
